package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.BannerDO;
import com.triones.ljr.entity.dto.IndustryDO;
import com.triones.ljr.entity.vo.BannerVO;
import com.triones.ljr.entity.vo.IndustryVO;
import com.triones.ljr.mapper.IndustryMapper;
import com.triones.ljr.service.IndustryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 行业分类
 */
@Service
@RequiredArgsConstructor
public class IndustryServiceImpl implements IndustryService {

    private final IndustryMapper industryMapper;

    @Override
    public List<IndustryVO> industryList() {
        return industryMapper.selectVoList(new LambdaQueryWrapper<IndustryDO>()
            .eq(IndustryDO::getIsDel, "0")
            .orderByAsc(IndustryDO::getSort));
    }

    @Override
    public TableDataInfo<IndustryVO> pageList(String keyword, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustryDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(IndustryDO::getIsDel,"0")
            .like(StringUtils.isNotBlank(keyword), IndustryDO::getName, keyword)
            .orderByAsc(IndustryDO::getSort);
        Page<IndustryVO> result = industryMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean add(IndustryDO dto) {
        return industryMapper.insert(dto) > 0;
    }

    @Override
    public boolean edit(IndustryDO dto) {
        return industryMapper.updateById(dto) > 0;
    }

    @Override
    public IndustryVO getInfo(Long id) {
        return industryMapper.selectVoById(id);
    }

    @Override
    public boolean remove(Long id) {
        IndustryDO industryDO = industryMapper.selectById(id);
        if (industryDO == null) {
            throw new ServiceException("信息不存在");
        }
        industryDO.setIsDel("1");
        return industryMapper.updateById(industryDO) > 0;
    }

    @Override
    public boolean checkNameUnique(IndustryDO dto) {
        boolean exist = industryMapper.exists(new LambdaQueryWrapper<IndustryDO>()
            .eq(IndustryDO::getName, dto.getName())
            .eq(IndustryDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), IndustryDO::getId, dto.getId()));
        return !exist;
    }
}
